import request from "@/utils/request.js";
import {desDecrypt, genDesKey, rsaPubEncrypt} from "@/utils/EncryptUtils.js";
import useAccountStore from "@/store/modules/AccountStore.js";
import {decryptByDes} from "@/utils/DesUtils.js";
import {del, post} from "@/utils/http.js";
import {elMessageTip} from "@/utils/MessageTipUtils.js";
import {resolvePromise_data, resolvePromise_status} from "@/utils/ReturnUtils.js";

/**
 * 分页获取允许登录的邮箱
 *
 * @param current 第几页
 * @param pageSize 每个数据个数
 * @param disabled 是否禁用的邮箱
 * @param match 模糊匹配条件
 */
export const getAllowMailsPageAPI = (current, pageSize, disabled = null, match = null) => {
    const desKey = genDesKey()
    return request.get(
        '/manager/allow/mails',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey),
                current, pageSize, disabled, match
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            return resolvePromise_data(res)
        }
    )
}

export const addAllowMailAPI = (mail, disabled) => {
    return post(
        '/manager/allow/mails/add',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            mail: rsaPubEncrypt(mail),
            disabled
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const delAllowMailAPI = (id) => {
    return post(
        '/manager/allow/mails/del',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const updateAllowMailAPI = (id, disabled) => {
    return post(
        '/manager/allow/mails/update',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id, disabled
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 获取全部权限列表
 *
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getAllAuthorityAPI = () => {
    const desKey = genDesKey()
    return request.get(
        '/manager/authority/all',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey)
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            console.log(res)
        }
    )
}

/**
 * 分页获取权限列表
 *
 * @param current
 * @param pageSize
 * @param disabled
 * @param nameMatch
 * @param codeMatch
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getAuthorityPageAPI = (current, pageSize, disabled = null, nameMatch = null, codeMatch = null) => {
    const desKey = genDesKey()
    return request.get(
        '/manager/authority/page',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey),
                current, pageSize,
                disabled: disabled === null || disabled === '' ? null : disabled,
                nameMatch: nameMatch === null || nameMatch === '' ? null : rsaPubEncrypt(nameMatch),
                codeMatch: codeMatch === null || codeMatch === '' ? null : rsaPubEncrypt(codeMatch)
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

/**
 * 新增权限
 *
 * @param name
 * @param code
 * @param disabled
 * @param desc
 * @returns {Promise<axios.AxiosResponse<*>>}
 */
export const addAuthorityAPI = (name, code, disabled, desc) => {
    return post(
        '/manager/authority',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            name: rsaPubEncrypt(name),
            code: rsaPubEncrypt(code),
            disabled,
            desc: rsaPubEncrypt(desc)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 新增权限
 *
 * @param id
 * @param name
 * @param code
 * @param disabled
 * @param desc
 * @returns {Promise<axios.AxiosResponse<*>>}
 */
export const updateAuthorityAPI = (id, name, code, disabled, desc) => {
    return request.put(
        '/manager/authority',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id,
            name: rsaPubEncrypt(name),
            code: rsaPubEncrypt(code),
            disabled,
            description: rsaPubEncrypt(desc)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 删除权限
 *
 * @param id
 * @returns {Promise<axios.AxiosResponse<*>>}
 */
export const delAuthorityAPI = (id) => {
    return del(
        '/manager/authority',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 获取用户全部信息
 */
export const getAdminAllInfo = () => {
    const desKey = genDesKey()
    return request.get(
        '/admin/all',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey)
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updateAvatarAPI = (avatar) => {
    const desKey = genDesKey()
    return post(
        '/admin/avatar',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            desKey: rsaPubEncrypt(desKey),
            avatar: rsaPubEncrypt(avatar),
            id: useAccountStore().manager.id
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updateUsernameAPI = (username) => {
    const desKey = genDesKey()
    return post(
        '/admin/username',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            desKey: rsaPubEncrypt(desKey),
            username: rsaPubEncrypt(username),
            id: useAccountStore().manager.id
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updatePwdAPI = (pwd) => {
    return post(
        '/admin/pwd',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id: useAccountStore().manager.id,
            pwd: rsaPubEncrypt(pwd)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}